package com.example.class_manage.Controller;

import com.example.class_manage.Entity.User;
import com.example.class_manage.Entity.serial_homework;
import com.example.class_manage.Mapper.All_HomeworkQuestion;
import com.example.class_manage.Mapper.Usermapper;
import com.example.class_manage.Util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Slf4j
@RequestMapping("/All_Homework")
@RestController
@Api(tags = "所有作业描述和作业id查询和发布作业")
public class All_HomeWork_Controller {
    @Autowired
    All_HomeworkQuestion all_homeworkQuestion;
    @Autowired
    Usermapper usermapper;

    @GetMapping("/Find_Homework_descirbe")
    public JsonData Find_Homework_descirbe(@ApiParam("作业号") @RequestParam("Id") String id) {
        serial_homework target = all_homeworkQuestion.Get_Homework_Byid(id);
        return target != null ? JsonData.buildSuccess(target, "查询成功") : JsonData.buildError("查询失败");
    }

    @GetMapping("/Find_Homeworkpic_descirbe_byteacher")
    public JsonData Get_teacher_Allhomeworkpic(@ApiParam("老师id号") @RequestParam("teacher_Id") String teacher_Id) {
        List<serial_homework> target = all_homeworkQuestion.Get_teacher_Allhomework_pic(teacher_Id);
        return JsonData.buildSuccess(target, "查询成功");
    }
    @GetMapping("/Find_Homeworktext_descirbe_byteacher")
    public JsonData Get_teacher_Allhomeworktext(@ApiParam("老师id号") @RequestParam("teacher_Id") String teacher_Id) {
        List<serial_homework> target = all_homeworkQuestion.Get_teacher_Allhomework_text(teacher_Id);
        return JsonData.buildSuccess(target, "查询成功");
    }
    //老师上传作业问题
    @PostMapping("/UpHomework_pic_byteacher")
    public JsonData UpHomework_pic_byteacher(@ApiParam(value = "作业id", required = true) @RequestParam String id,
                                             @ApiParam("老师id号") @RequestParam("teacher_Id") String teacher_Id,
                                             @ApiParam("问题描述") @RequestParam("describe") String describe,
                                             @ApiParam(value = "问题照片(可以为空)", required = false) @RequestParam MultipartFile file,
                                             @ApiParam(value = "班级号", required = true) @RequestParam String sclass,
                                             @ApiParam(value = "年纪") @RequestParam String grade
    ) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//标准化时间
        all_homeworkQuestion.Uprecord_homework_pic(id, teacher_Id, describe, sdf.format(date));
        log.info("问题更新完毕");
        List<User> users = usermapper.queryUserlist(sclass, grade);
        users.forEach(item ->
        {
            System.out.println(item.getId());
            if(item.getFlag()!=2)
            {}
            else
            {
                byte[] data = new byte[0];
                try {
                    data = file.getBytes();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                    all_homeworkQuestion.insert_question_pic_bysclass(id, describe, data, "0", sclass, String.valueOf(item.getId()));


            }
            });
        return JsonData.buildSuccess("ok");
    }

    @Transactional
    @PostMapping("/UpHomework_text_byteacher")
    public JsonData UpHomework_text_byteacher(@ApiParam(value = "作业id", required = true) @RequestParam String id,
                                              @ApiParam("老师id号") @RequestParam("teacher_Id") String teacher_Id,
                                              @ApiParam("问题描述") @RequestParam("describe") String describe,
                                              @ApiParam(value = "问题照片(可以为空)", required = false) @RequestParam MultipartFile file,
                                              @ApiParam(value = "班级号", required = true) @RequestParam String sclass,
                                              @ApiParam(value = "年纪") @RequestParam String grade
    ) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//标准化时间
        all_homeworkQuestion.Uprecord_homework_text(id, teacher_Id, describe, sdf.format(date));
        log.info("问题更新完毕");
        List<User> users = usermapper.queryUserlist(sclass, grade);
        users.forEach(item ->
        {
            if (item.getFlag() != 2) {
            } else {
                byte[] data = new byte[0];
                try {
                    data = file.getBytes();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    all_homeworkQuestion.insert_question_text_bysclass(id, describe, data, "0", sclass, String.valueOf(item.getId()));
                }
            }
        });
        return JsonData.buildSuccess("ok");
    }

}
